/**
 * 入参
 *
 */
import React, { PureComponent } from 'react';
import { TreeVEditChartsC } from '../../../../comps';
import style from '../index.less';
import EditConfigC from './editConfigC';
import { connect } from 'dva';

class DetailC extends PureComponent {
  state = {
    status: '1', //1-详情状态   2-编辑状态
    portfolioCode: undefined, //当前选中组合Code
    treeList: [], //组合树列表
    nodeName: undefined, //节点名称
  };
  componentDidMount() {
    this.setState(
      {
        //默认根节点为选中节点
        portfolioCode: this.props.rootItem.value,
        nodeName: this.props.rootItem.name,
      },
      () => {
        this.getTree(); //查询组合树
      },
    );
  }
  getTree = (type, obj) => {
    //查询组合树
    const { dispatch } = this.props;
    dispatch({
      type: 'portfolioDefinitionM/queryTreeList',
      payload: {
        id: this.props.rootId,
      },
      callback: (data) => {
        this.setState(
          {
            treeList: data || [],
          },
          () => {
            if (type == 'refresh') {
              this.state.TreeVEditChartsC.selectNode(obj); //新增节点成功之后 定位到新增节点
            } else {
              this.state.TreeVEditChartsC.selectNode(this.props.rootItem); //默认根节点
            }
          },
        );
      },
    });
  };
  //删除成功重新初始化组合定义详情页
  onDelete = () => {
    this.getTree();
  };
  //新增子节点成功 刷新页面 并定位在新增的位置
  onAdd = (obj) => {
    this.setState(
      {
        //查询组合树
        pstortfolioCode: obj.value,
        status: '1',
      },
      () => {
        if (this.props.type == 'workStation') {
          //当时非标工作台跳转，新增成功后非标工作台需要刷新
          this.props.addPortfolio();
        }
        this.getTree('refresh', obj);
      },
    ); //刷新组合树
  };
  render() {
    const { status, portfolioCode, treeList } = this.state;
    return (
      <div style={{ height: '100%' }} className={style.detail}>
        <div>
          <TreeVEditChartsC
            data={treeList}
            height={'100%'}
            selectedOnChange={(e) => {
              this.setState({
                portfolioCode: e ? e.value : undefined,
                nodeName: e ? e.name : undefined,
                status: '1', //变为详情状态
              });
            }}
            onRef={(ref) => this.setState({ TreeVEditChartsC: ref })}
          />
        </div>
        <div>
          <EditConfigC
            onRef={(ref) => {
              this.childRef = ref;
            }}
            portfolioCode={portfolioCode}
            type={status}
            onClose={this.props.onClose}
            onDelete={this.onDelete}
            onAdd={this.onAdd}
            treeList={treeList.length ? treeList[0] : {}}
            rootId={this.props.rootId}
            rootCode={this.props.rootItem.value} //根节点code 用户判断根节点能否删除
            isInvestment={this.props.rootItem.isInvestment} // 根据产品判断是否可以投顾
            nodeName={this.state.nodeName}
            isExtra={this.props.rootItem.isExtra} //是否有外部通道
            tradeMode={this.props.rootItem.tradeMode} //交易模式
          />
        </div>
      </div>
    );
  }
}
export default connect(({ portfolioDefinitionM, userM: user, loading }) => ({
  portfolioDefinitionM,
  user,
  loading,
}))(DetailC);
